package com.audaque.encoding.util;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.Random;

/**
 * @author mayong
 * @since 2024/4/14
 */
@UtilityClass
@Slf4j
public class FileUtil {

    public static void createPathIfNotExists(String directoryPath) {
        File directory = new File(directoryPath);
        // 如果目录不存在，则创建
        if (!directory.exists()) {
            if (!directory.mkdirs()) {
                log.error("无法创建目录：{}", directoryPath);
            }
        }
    }

    public static String checkExistAndRandom(String directoryPath, String filename) {
        String filepath = directoryPath + File.separator + filename;
        File file = new File(filepath);
        if (file.exists()) {
            // 获取文件名（不含扩展名）
            String baseName = filename.substring(0, filename.lastIndexOf('.'));
            String suffix = filename.substring(filename.lastIndexOf('.') + 1);
            filepath = directoryPath + File.separator + baseName + "_" + (new Random().nextInt(90) + 10) + "." + suffix;
        }
        return filepath;
    }

}



